package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-06-20 15:28:37
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{

    @Autowired
    TbTypeMapper tbTypeMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R getTreeList() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeStatus", type.getTypeStatus());
        });
        return R.ok(treeList);
    }

    @Override
    public R addType(TbType tbType) {
        TbType type = tbTypeMapper.selectOne(
                new LambdaQueryWrapper<TbType>().eq(TbType::getTypeName, tbType.getTypeName())
        );
        if(type != null){
            return R.error(500,"该类型已经存在");
        }

        String token = request.getHeader("token");

        String userName = TokenUtils.getUserName(token);

        tbType.setCreateBy(userName);
        tbType.setCreateTime(new Date());
        tbTypeMapper.insert(tbType);

        return R.ok();
    }

    @Override
    public R uploadImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.ok(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error(500,"上传失败");
    }

    @Override
    public R getSelectTree() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");

            tree.putExtra("value",type.getTypeId());
            tree.putExtra("label",type.getTypeName());
        });
        return R.ok(treeList);
    }
}




